package com.mutouren.web.sso;

import java.util.concurrent.TimeUnit;

import com.google.common.cache.Cache;
import com.mutouren.common.cache.local.GuavaCacheUtils;
import com.mutouren.common.cache.local.GuavaCacheUtils.ExpireType;
import com.mutouren.common.session.UserInfo;
import com.mutouren.common.utils.UUIDUtils;

public class TicketManager {
	
	private static Cache<String, UserInfo> cache = 
			GuavaCacheUtils.createCache(60 * 10, TimeUnit.SECONDS, ExpireType.expireAfterWrite);
	
	public static String createTicket(UserInfo userInfo) {
		String ticket = UUIDUtils.createBase64UUID();
		cache.put(ticket, userInfo);
		return ticket;
	}
	
	public static UserInfo getUserInfo(String ticket) {
		UserInfo result = cache.getIfPresent(ticket);
		if (result != null) {
			cache.invalidate(ticket);
		}
		return result;
	}
	
}
